/**
 * @Author     :lanbery
 * @e-mail     : lanbery@gmail.com
 * @version    : V1.0
 * @createtime : 2011-11-16-下午03:37:17
 */
package com.lambor.common.util;

import java.util.Calendar;
import java.util.Date;

/**
 * @about this class :
 * @author           ： lanbery
 * @e-mail           : lanbery@gmail.com
 * @version          : V1.0
 * @modifytime       : 2011-11-16-下午03:37:17
 * @filepath         : SSI-com.lambor.common.util//DateHelper.java
 *
 */
public class DateHelper {
	// 日志
	
	
	// 中文数字
	private static final String s1[] = { "О", "一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二" };
	
	/**
	 * 
	 * @method annotations: 获取当前日期[yyyy-mm-dd]
	 * @creator           :lanbery
	 * @createtime        : 2011-11-16-下午03:59:33 
	 * @param split       :[-或/]
	 * @return            :String
	 * DateHelper.method()
	 *
	 */
	public static String currentDate(String split){
		String d = currentDate();
		if(split.equals("-")||split.equals("/")){
			d = d.substring(0, 4)+split+d.substring(4,6)+split+d.substring(6,8);
		}
		return d;
	}
	/**
	 * 
	 * @method annotations: 获取当前日期时间[yyyy-mm-dd HH:mm:ss]
	 * @creator           :lanbery
	 * @createtime        : 2011-11-16-下午04:04:22 
	 * @param split       :[- 或 /]
	 * @return            :String
	 * DateHelper.method()
	 *
	 */
	public static String currentDateTime(String split){
		String d = currentDateTime();
		if(split.equals("-")||split.equals("/")){
			d = d.substring(0, 4)+split+d.substring(4,6)+split+d.substring(6,8)
			+" "+d.substring(8, 10)+":"+d.substring(10, 12)+":"+d.substring(12, 14);
		}
		return d;
	}
	/**
	 * 
	 * @method annotations: 当前日期[yyyymmdd]
	 * @creator           :lanbery
	 * @createtime        : 2011-11-16-下午04:05:40 
	 * @return            :String
	 * DateHelper.method()
	 *
	 */
	public static String currentDate()
	{
	   	Calendar cal = Calendar.getInstance();
	   	long val = cal.get(Calendar.YEAR) * 10000 + 
	   		(cal.get(Calendar.MONTH)+1) * 100 + 
	   		cal.get(Calendar.DAY_OF_MONTH);
		return String.valueOf(val);
	}
	
	/**
	 * 
	 * @method annotations:	取当前日期的偏移日期
	 * @creator           :lanbery
	 * @createtime        : 2011-11-16-下午04:06:07 
	 * @param days        :日数量
	 * @return            :String
	 * DateHelper.method()
	 *
	 */
	public static String afterDate( int days )
	{
		// 当前日期
		Calendar cal = Calendar.getInstance();
		
		// 计算偏移
		cal.add( Calendar.DATE, days );
	   	
	   	long val = cal.get(Calendar.YEAR) * 10000 + 
	   		(cal.get(Calendar.MONTH)+1) * 100 + 
	   		cal.get(Calendar.DAY_OF_MONTH);
		return String.valueOf(val);
	}
	
	/**
	 * 计算下一个日期
	 * @param date 日期字符串[yyyymmdd]
	 * @param days 日数量
	 * @return
	 */
	public static String afterDate( String date, int days )
	{
		// 当前日期
		Calendar cal = Calendar.getInstance();
		try{
			cal.set( Calendar.YEAR, Integer.parseInt(date.substring(0,4)) );
			cal.set( Calendar.MONTH, Integer.parseInt(date.substring(4,6))-1 );
			cal.set( Calendar.DAY_OF_MONTH, Integer.parseInt(date.substring(6,8)) );
		}
		catch( Throwable e ){
		}
		
		// 计算偏移
		cal.add( Calendar.DATE, days );
	   	long val = cal.get(Calendar.YEAR) * 10000 + 
	   		(cal.get(Calendar.MONTH)+1) * 100 + 
	   		cal.get(Calendar.DAY_OF_MONTH);
		return String.valueOf(val);
	}

	/**
	 * 取当前日期的偏移日期
	 * @param months 月数量
	 * @return
	 */
	public static String afterMonth( int months )
	{
		// 当前日期
		Calendar cal = Calendar.getInstance();
		int day = cal.get(Calendar.DAY_OF_MONTH);
		cal.set( Calendar.DAY_OF_MONTH, 1 );
		
		// 计算偏移
		cal.add( Calendar.MONTH, months );
		
		// 取月的最大天数
		int day1 = cal.getActualMaximum( Calendar.DAY_OF_MONTH );
		if( day > day1 ){
			day = day1;
		}
	   	
	   	long val = cal.get(Calendar.YEAR) * 10000 + 
	   		(cal.get(Calendar.MONTH)+1) * 100 + 
	   		day;
	   	
		return String.valueOf(val);
	}
	
	/**
	 * 计算下一个日期
	 * @param date 日期字符串[yyyymmdd]
	 * @param months 月数量
	 * @return
	 */
	public static String afterMonth( String date, int months )
	{
		// 当前日期
		Calendar cal = Calendar.getInstance();
		
		int day = 0;
		try{
			cal.set( Calendar.YEAR, Integer.parseInt(date.substring(0,4)) );
			cal.set( Calendar.MONTH, Integer.parseInt(date.substring(4,6))-1 );
			cal.set( Calendar.DAY_OF_MONTH, 1 );
			
			// 计算偏移
			cal.add( Calendar.MONTH, months );
			
			// 取月的最大天数
			int day1 = cal.getActualMaximum( Calendar.DAY_OF_MONTH );
			day = Integer.parseInt(date.substring(6,8));
			if( day > day1 ){
				day = day1;
			}
		}
		catch( Throwable e ){
			
		}
	   	
	   	long val = cal.get(Calendar.YEAR) * 10000 + 
	   		(cal.get(Calendar.MONTH)+1) * 100 + 
	   		day;
	   	
		return String.valueOf(val);
	}
	
	/**
	 * 当前时间[hhMMss]
	 * @return
	 */
	public static String currentTime( ) 
	{
		String result;
		Calendar cal = Calendar.getInstance();
		
		long val = cal.get( Calendar.HOUR_OF_DAY ) * 10000 +
			cal.get( Calendar.MINUTE ) * 100 +
			cal.get( Calendar.SECOND );
		
		if( val < 10000 ){
			result = "00" + String.valueOf(val);
		}
		else if( val < 100000 ){
			result = "0" + String.valueOf(val);
		}
		else{
			result = String.valueOf(val);
		}
		return result;
	}
	
	/**
	 * 日期和时间[yyyymmddhhMMss]
	 * @return
	 */
	public static String currentDateTime( ) 
	{
		Calendar cal = Calendar.getInstance();
		
		// 日期
		long val = cal.get(Calendar.YEAR) * 10000 + 
			(cal.get(Calendar.MONTH) + 1) * 100 +
			cal.get(Calendar.DAY_OF_MONTH);
		
		// 时间
		long val1 = cal.get( Calendar.HOUR_OF_DAY ) * 10000 +
			cal.get( Calendar.MINUTE ) * 100 +
			cal.get( Calendar.SECOND );
		
		String result;
		if( val1 < 10000 ){
			result = String.valueOf(val) + "00" + String.valueOf(val1);
		}
		else if( val1 < 100000 ){
			result = String.valueOf(val) + "0" + String.valueOf(val1);
		}
		else{
			result = String.valueOf(val) + String.valueOf(val1);
		}
		
		return result;
	}
	
	/**
	 * 计算时间
	 * date 日数量
	 * @return
	 */
	public static String afterDateTime( int date ) 
	{
		Calendar cal = Calendar.getInstance();
		
		// 计算偏移
		cal.add( Calendar.DATE, date );

		// 日期
		long val = cal.get(Calendar.YEAR) * 10000 + 
			(cal.get(Calendar.MONTH) + 1) * 100 +
			cal.get(Calendar.DAY_OF_MONTH);
		
		// 时间
		long val1 = cal.get( Calendar.HOUR_OF_DAY ) * 10000 +
			cal.get( Calendar.MINUTE ) * 100 +
			cal.get( Calendar.SECOND );
		

		if( val1 < 10000 ){
			return String.valueOf(val) + "00" + String.valueOf(val1);
		}
		else if( val1 < 100000 ){
			return String.valueOf(val) + "0" + String.valueOf(val1);
		}
		else{
			return String.valueOf(val) + String.valueOf(val1);
		} 
	}
	
	/**
	 * 取日期[yyyymmdd]
	 * @param date 日期
	 * @return
	 */
	public static String getDate( Date date )
	{
		Calendar cal = Calendar.getInstance();
		cal.setTime( date );
	   	long val = cal.get(Calendar.YEAR) * 10000 + 
	   		(cal.get(Calendar.MONTH)+1) * 100 + 
	   		cal.get(Calendar.DAY_OF_MONTH);
		return String.valueOf(val);
	}
	
	/**
	 * 取日期和时间[yyyymmddhhMMss]
	 * @param date 日期
	 * @return
	 */
	public static String getDateTime( Date date )
	{
		Calendar cal = Calendar.getInstance();
		cal.setTime( date );
		
		// 日期
		long val = cal.get(Calendar.YEAR) * 10000 + 
			(cal.get(Calendar.MONTH) + 1) * 100 +
			cal.get(Calendar.DAY_OF_MONTH);
		
		// 时间
		long val1 = cal.get( Calendar.HOUR_OF_DAY ) * 10000 +
			cal.get( Calendar.MINUTE ) * 100 +
			cal.get( Calendar.SECOND );
		
		if( val1 < 10000 ){
			return String.valueOf(val) + "00" + String.valueOf(val1);
		}
		else if( val1 < 100000 ){
			return String.valueOf(val) + "0" + String.valueOf(val1);
		}
		else{
			return String.valueOf(val) + String.valueOf(val1);
		}
	}
	
	/**
	 * 生成日期
	 * @param source [yyyymmdd]、[hhMMss]、[yyyymmddhhMMss]转换成日期
	 * @return
	 */
	public static Date parse( String source )
	{
		try{
			int len = source.length();
			if( len == 8 ){
				Calendar cal = Calendar.getInstance();

				// 设置年月日
				int y = Integer.parseInt( source.substring(0, 4) );
				int m = Integer.parseInt( source.substring(4, 6) );
				int d = Integer.parseInt( source.substring(6, 8) );
				cal.set( y, m-1, d );
				
				return cal.getTime();
			}
			else if( len == 6 ){
				Calendar cal = Calendar.getInstance();

				// 设置时间
				int h = Integer.parseInt( source.substring(0, 2) );
				int m = Integer.parseInt( source.substring(2, 4) );
				int s = Integer.parseInt( source.substring(4, 6) );
				cal.set( Calendar.HOUR_OF_DAY, h );
				cal.set( Calendar.MINUTE, m );
				cal.set( Calendar.SECOND, s );
				
				return cal.getTime();
			}
			else if( len == 14 ){
				Calendar cal = Calendar.getInstance();

				// 设置年月日、时分秒
				int y = Integer.parseInt( source.substring(0, 4) );
				int m = Integer.parseInt( source.substring(4, 6) );
				int d = Integer.parseInt( source.substring(6, 8) );
				int hh = Integer.parseInt( source.substring(8, 10) );
				int mm = Integer.parseInt( source.substring(10, 12) );
				int ss = Integer.parseInt( source.substring(12, 14) );
				cal.set( y, m-1, d, hh, mm, ss );
				
				return cal.getTime();
			}
			else{
				return null;
			}
		}
		catch( Throwable e ){
			return null;
		}
	}
	
	/**
	 * 生成日期[yyyy年mm月dd日]
	 * @param source 日期
	 * @return
	 */
	public static String convertChineseDate( String source )
	{
		try{
			String y, d;
			int m;
			int len = source.length();
			if( len == 8 || len == 14 ){
				// 取年月日
				y = source.substring(0, 4);
				m = Integer.parseInt( source.substring(4, 6) );
				d = source.substring(6, 8);
				
				// 保存结果
				StringBuilder result = new StringBuilder( 64 );
				
				// 年
				int n1, n2;
				for( int ii=0; ii<4; ii++ ){
					n1 = y.charAt(ii) - '0';
					result.append( s1[n1] );
				}
				
				result.append( "年" );
				
				// 月
				if( m > 12 || m < 1 ){
					//TODO Exception 日期的格式错误，月只能是[1~12]
				}
				
				result.append( s1[m] );
				result.append( "月" );
				
				// 日
				n1 = Integer.parseInt( d );
				if( n1 > 31 || n1 < 1 ){
					//TODO Exception日期的格式错误，日只能是[1~31]
				
				}
				
				n1 = d.charAt(0) - '0';
				n2 = d.charAt(1) - '0';
				if( n1 == 1 ){
					result.append( "十" );
				}
				else if( n1 == 2 ){
					result.append( "二" );
				}
				else if( n1 == 3 ){
					result.append( "三" );
				}
				
				if( n2 > 0 ){
					result.append( s1[n2] );
				}
				else if( n1 > 1 ){
					result.append( "十" );
				}
				
				result.append( "日" );
				
				return result.toString();
			}
			else{
				return null;
			}
		}
		catch( Throwable e ){
			return null;
		}
	}

	/**
	 * 当前年
	 * @return
	 */
	public static String longYear( ) {
		return longYear(System.currentTimeMillis());
	}

	/**
	 * 毫秒转换成年
	 * @param millis 毫秒
	 * @return
	 */	
	public static String longYear(long millis ) {
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis( millis );
		return String.valueOf(cal.get(Calendar.YEAR));
	}

	/**
	 * 当前年月
	 * @return
	 */
	public static String longYearMonth() {
		return longYearMonth(System.currentTimeMillis());
	}
	
	/**
	 * 毫秒转换成年月[yyyy/mm]
	 * @param millis 毫秒
	 * @return
	 */	
	public static String longYearMonth(long millis ) {
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis( millis );
		return longYearMonth( cal );
	}

	private static String longYearMonth( Calendar cal ){
		int	v;
		StringBuilder result = new StringBuilder(7);
		// 年
		v = cal.get(Calendar.YEAR);
		result.append( v );
		result.append( "/" );
		
		// 月
		v = cal.get(Calendar.MONTH) + 1;
		if( v < 10 ){
			result.append( "0" );
		}
		result.append( v );
		
		return result.toString();
	}
	
	
	/**
	 * 当前年月日
	 * @return
	 */
	public static String longYearMonthDate() {
		return longYearMonth(System.currentTimeMillis());
	}
	
	/**
	 * 毫秒转换成年月[yyyy/mm/dd]
	 * @param millis 毫秒
	 * @return
	 */	
	public static String longYearMonthDate(long millis ) {
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis( millis );
		return longYearMonthDate( cal );
	}
	private static String longYearMonthDate( Calendar cal ){
		int	v;
		StringBuilder result = new StringBuilder( 32 );
		
		// 年
		v = cal.get(Calendar.YEAR);
		result.append( v );
		result.append( "/" );
		
		// 月
		v = cal.get(Calendar.MONTH) + 1;
		if( v < 10 ){
			result.append( "0" );
		}

		result.append( v );
		result.append( "/" );
		
		// 日
		v = cal.get(Calendar.DAY_OF_MONTH);
		if( v < 10 ){
			result.append( "0" );
		}
		
		result.append( v );
		
		return result.toString();
	}	
	
	/**
	 * 当前日期[yyyy/mm/dd hh:MM:ss]
	 * @return
	 */
	public static String longDateTime( ) 
	{
		Calendar cal = Calendar.getInstance();
		return longDateTime( cal );
	}
	
	/**
	 * 毫秒转换成日期[yyyy/mm/dd hh:MM:ss]
	 * @param millis 毫秒
	 * @return
	 */
	public static String longDateTime( long millis ) 
	{
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis( millis );
		return longDateTime( cal );
	}
	
	private static String longDateTime( Calendar cal ) 
	{
		int	v;
		StringBuilder result = new StringBuilder( 32 );
		
		// 年
		v = cal.get(Calendar.YEAR);
		result.append( v );
		result.append( "/" );
		
		// 月
		v = cal.get(Calendar.MONTH) + 1;
		if( v < 10 ){
			result.append( "0" );
		}

		result.append( v );
		result.append( "/" );
		
		// 日
		v = cal.get(Calendar.DAY_OF_MONTH);
		if( v < 10 ){
			result.append( "0" );
		}
		
		result.append( v );
		result.append( " " );
		
		// 时
		v = cal.get(Calendar.HOUR_OF_DAY);
		if( v < 10 ){
			result.append( "0" );
		}
		
		result.append( v );
		result.append( ":" );
		
		// 分
		v = cal.get(Calendar.MINUTE);
		if( v < 10 ){
			result.append( "0" );
		}
		
		result.append( v );
		result.append( ":" );
		
		// 秒
		v = cal.get(Calendar.SECOND);
		if( v < 10 ){
			result.append( "0" );
		}
		
		result.append( v );
		result.append( " " );
		
		// 毫秒
		v = cal.get( Calendar.MILLISECOND );
		if( v < 10 ){
			result.append( "00" );
		}
		else if( v < 100 ){
			result.append( "0" );
		}
		
		result.append( v );
		
		return result.toString();
	}

	/**
	 * 当临时文件路径[yyyymmdd/hh/]
	 * @return
	 */
	public static String getTempPath()
	{
	   	Calendar cal = Calendar.getInstance();
	   	long val = cal.get(Calendar.YEAR) * 10000 + 
	   		(cal.get(Calendar.MONTH)+1) * 100 + 
	   		cal.get(Calendar.DAY_OF_MONTH);
	   	
	   	long val1 = cal.get(Calendar.MINUTE) * 100000 +
	   		cal.get(Calendar.SECOND) * 1000 +
	   		cal.get( Calendar.MILLISECOND );
			
		return String.valueOf(val) + "/" + cal.get(Calendar.HOUR_OF_DAY) + "/" + val1;
	}
	
	public static void main( String[] args )
	{
//		try{
//			System.out.println( "afterDate=" + afterMonth("20080130", 1) );
//			System.out.println( "afterDate=" + afterDate("20081229", 10) );
//			System.out.println( "long time = " + longDateTime() );
//			Thread.sleep( 100 );
//			System.out.println( "long time = " + longDateTime(Calendar.getInstance().getTimeInMillis()) );
//		}
//		catch( Throwable e ){
//			e.printStackTrace();
//		}
		
		System.out.println(""+currentDateTime("-"));
	}
}
